<template>
	<view v-if="list.length>0">
		<block v-for="(item,index) in list" :key="index">
			<msg-list :item="item" :index="index"></msg-list>
		</block>
		<uni-popup ref="popup" type="top">
			<view 
			class="flex align-center justify-center font-md border-bottom py-2" 
			hover-class="bg-light" 
			@click="popupEvent('friend')">
				<text class="iconfont icon-sousuo mr-2"></text> 推荐好友
			</view>
			<view class="flex align-center justify-center font-md py-2" 
			hover-class="bg-light" 
			@click="popupEvent('clear')">
				<text class="iconfont icon-shanchu mr-2"></text> 清除列表	
			</view>
			
		</uni-popup>
	</view>
	<view v-else>
		<no-thing></no-thing>
		<uni-popup ref="popup" type="top">
			<view 
			class="flex align-center justify-center font-md border-bottom py-2" 
			hover-class="bg-light" 
			@click="popupEvent('friend')">
				<text class="iconfont icon-sousuo mr-2"></text> 推荐好友
			</view>
			<view class="flex align-center justify-center font-md py-2" 
			hover-class="bg-light" 
			@click="popupEvent('clear')">
				<text class="iconfont icon-shanchu mr-2"></text> 清除列表	
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import msgList from '@/components/msg/msg-list'
	import uniPopup from '@/components/uni-ui/uni-popup/uni-popup'
	import {getNews} from "@/network/news.js";
	export default {
		components:{
			msgList,
			uniPopup
		},
		data() {
			return {
				list:[]
			}
		},

		beforeDestroy() {
			uni.$off('MsgList')
			uni.$off('UserBack')
			uni.$off('Logout',(e=>{}))
		},
		onLoad() {
			this.getNewsList()
			uni.$on("UserBack",res=>{
				let index = this.list.findIndex(item => {
				        return item.user==res.user || item.receive_user == res.user
				});
				let item = this.list[index]
				item.content = res.content
				item.create_date = res.create_date
				item.user = res.user
				item.receive_user = res.receive_user
				item.unread = 0
				
				return
			})
			uni.$on("MsgList",(res)=>{
				let index = this.list.findIndex(item => {
				        return item.user==res.user || item.receive_user == res.user
				});
				if(res.code ==201){
					// 保存好返回来的值
					let item = this.list[index]
					item.content = res.content
					item.create_date = res.create_date
					item.user = res.user
					item.receive_user = res.receive_user
					item.unread+=1
					console.log(item)
					return
				}
			})
			uni.$on("Logout",()=>{
				this.getNewsList()
				this.$store.dispatch("totalNoread")
			})
			
			
			
		},
		onNavigationBarButtonTap(e){
			switch (e.index){
				case 0: // 左边
				this.checkAuth(()=>{
					uni.navigateTo({
						url:"/pages/user-list/user-list"
					})
				})
				break;
				// 关闭弹出层	
				
				case 1:
				this.$refs.popup.open()
				break;
			}
		},
		
		onPullDownRefresh(){
			this.refresh()
		},
		methods:{
			// 下拉刷新
			refresh(){
				setTimeout(()=>{
					// 停止下拉刷新
					uni.stopPullDownRefresh()
				},2000)
			},
			getNewsList(){
				getNews().then(attr=>{
					let [err,res] = attr
					this.list=res.data
					
				})
			},
			// 弹出层点击事件
			popupEvent(e){
				switch(e){
					case 'friend':
					console.log('用户列表')
					
					break;
					case 'clear':
					console.log('清除列表')
					break;
				}
				this.$refs.popup.close()
			}
		}
		
	}
</script>

<style>
	
</style>
